package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.PostInfoActivity;
import com.example.city_home_app.Bean.Down_PostComment;
import com.example.city_home_app.Bean.Down_PostReply;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Bean.PostContent;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.MyCircleImageView;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.shaohui.bottomdialog.BottomDialog;

public class Adapter_PostComment extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Down_PostComment> list;
    private FragmentManager fragmentManager;
    private BottomDialog dialog;

    public Adapter_PostComment(Context context, FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
    }

    public void setList(List<Down_PostComment> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_post_comment, parent, false);
        Adapter_PostComment.ViewHolder viewHolder = new Adapter_PostComment.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_PostComment bean = list.get(position);
        if(bean == null){
            return;
        }
        final Adapter_PostComment.ViewHolder viewHolder = (Adapter_PostComment.ViewHolder) holder;
        viewHolder.tvRecitemPostcommentAuthor.setText(bean.getUser_name());
        viewHolder.tvRecitemPostcommentContent.setText(bean.getComment_content());
        viewHolder.tvRecitemPostcommentTime.setText(bean.getComment_time());
        viewHolder.layoutRecitemPostcommentComment.setVisibility(View.GONE);
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+bean.getUser_headimage())
                .into(viewHolder.imgRecitemPostcommentHaedimage);
        final Adapter_PostReply adapter_postReply;
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        viewHolder.recRecitemPostcommentComment.setLayoutManager(layoutManager);
        adapter_postReply = new Adapter_PostReply(context);
        viewHolder.recRecitemPostcommentComment.setAdapter(adapter_postReply);
        viewHolder.recRecitemPostcommentComment.setItemAnimator(new DefaultItemAnimator());
        final ApiModel model = new ApiModel();

        final RetrofitListener<List<Down_PostReply>> listRetrofitListener = new RetrofitListener<List<Down_PostReply>>() {
            @Override
            public void onResponse(List<Down_PostReply> listener) {
                if(listener.size()!=0){
                    viewHolder.layoutRecitemPostcommentComment.setVisibility(View.VISIBLE);
                    adapter_postReply.setList(listener);
                    adapter_postReply.notifyDataSetChanged();
                }else{
                    viewHolder.layoutRecitemPostcommentComment.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        final RetrofitListener<Down_Universe> replylistener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if(listener.getMessage().equals("success")){
                    UIUtils.showToast(context,"回复成功",UIUtils.LENGTH_SHORT);
                    model.getpostcommentreply(bean.getComment_id(),listRetrofitListener);
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        model.getpostcommentreply(bean.getComment_id(),listRetrofitListener);

        viewHolder.tvRecitemPostcommentAuthor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = BottomDialog.create(fragmentManager);
                dialog.setViewListener(new BottomDialog.ViewListener() {
                    @Override
                    public void bindView(final View v) {
                        final EditText mEditText = (EditText) v.findViewById(R.id.edittext_dialog_newscomment_content);
                        Button button = (Button) v.findViewById(R.id.btn_dialog_newscomment_send);
                        mEditText.post(new Runnable() {
                            @Override
                            public void run() {
                                InputMethodManager imm =
                                        (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.showSoftInput(mEditText, 0);
                            }
                        });
                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (!mEditText.getText().toString().equals("")) {
                                    dialog.dismiss();
                                    model.replypostcomment(bean.getComment_id(),BaseActivity.getUserinfo().getUserid(),
                                            mEditText.getText().toString(),bean.getPost_id(),replylistener);
                                } else {
                                    UIUtils.showToast(context, "格式非法", UIUtils.LENGTH_SHORT);
                                }
                            }
                        });
                    }
                })
                        .setLayoutRes(R.layout.newscomment_dialog)
                        .setDimAmount(0.1f)
                        .setCancelOutside(true)
                        .setTag("评论")
                        .show();
            }
        });

    }



    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_recitem_postcomment_haedimage)
        MyCircleImageView imgRecitemPostcommentHaedimage;
        @BindView(R.id.tv_recitem_postcomment_author)
        TextView tvRecitemPostcommentAuthor;
        @BindView(R.id.tv_recitem_postcomment_time)
        TextView tvRecitemPostcommentTime;
        @BindView(R.id.tv_recitem_postcomment_content)
        TextView tvRecitemPostcommentContent;
        @BindView(R.id.rec_recitem_postcomment_comment)
        RecyclerView recRecitemPostcommentComment;
        @BindView(R.id.layout_recitem_postcomment_comment)
        ConstraintLayout layoutRecitemPostcommentComment;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
